package by.koleda.benicetobunnies.provider;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import by.koleda.benicetobunnies.provider.BunniesContract.CompaniesColumns;

public class BunniesDatabase extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_NAME = "bunnies.db";

	interface Tables {
		public static final String COMPANIES = "companies";
	}
	
	private static final String CREATE_COMPANIES_TABLE = "CREATE TABLE "
			+ Tables.COMPANIES + " (" + CompaniesColumns._ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ CompaniesColumns.COMPANY_NAME + " TEXT, "
			+ CompaniesColumns.PARENT_COMPANY + " TEXT, "
			+ CompaniesColumns.ADDRESS + " TEXT, "
			+ CompaniesColumns.PHONE_NUMBER + " TEXT, " 
			+ CompaniesColumns.LINK + " TEXT)";
	
	private static final String DROP_COMPANIES_TABLE = "DROP TABLE IF EXISTS "
			+ Tables.COMPANIES;
	
	public BunniesDatabase(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	@Override
	public final void onCreate(final SQLiteDatabase db) {
		try {
			db.beginTransaction();
			db.execSQL(CREATE_COMPANIES_TABLE);
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}

	}

	@Override
	public final void onUpgrade(final SQLiteDatabase db, final int oldVersion,
			final int newVersion) {
		try {
			db.beginTransaction();
			db.execSQL(DROP_COMPANIES_TABLE);
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
		onCreate(db);
	}
}
